package com.jvtd.demo.im.bean;

import android.os.Parcel;
import android.text.TextUtils;

import com.jvtd.demo.im.ConversationListActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

@MessageTag(value = ConversationListActivity.INFO_NOTIFICATION_MSG, flag = MessageTag.ISCOUNTED)
public class MyInformationNotificationMessage extends MessageContent
{
  private static final String TAG = "MyInformationNotificationMessage";
  private String message;
  protected String extra;

  public static final Creator<MyInformationNotificationMessage> CREATOR = new Creator<MyInformationNotificationMessage>()
  {
    public MyInformationNotificationMessage createFromParcel(Parcel source)
    {
      return new MyInformationNotificationMessage(source);
    }

    public MyInformationNotificationMessage[] newArray(int size)
    {
      return new MyInformationNotificationMessage[size];
    }
  };

  public byte[] encode()
  {
    JSONObject jsonObj = new JSONObject();

    try
    {
      if (!TextUtils.isEmpty(this.getMessage()))
      {
        jsonObj.put("message", this.getMessage());
      }

      if (!TextUtils.isEmpty(this.getExtra()))
      {
        jsonObj.put("extra", this.getExtra());
      }

      if (this.getJSONUserInfo() != null)
      {
        jsonObj.putOpt("user", this.getJSONUserInfo());
      }
    } catch (JSONException var4)
    {
      RLog.e("MyInformationNotificationMessage", "JSONException " + var4.getMessage());
    }

    try
    {
      return jsonObj.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException var3)
    {
      var3.printStackTrace();
      return null;
    }
  }

  protected MyInformationNotificationMessage()
  {
  }

  public static MyInformationNotificationMessage obtain(String message)
  {
    MyInformationNotificationMessage model = new MyInformationNotificationMessage();
    model.setMessage(message);
    return model;
  }

  public MyInformationNotificationMessage(byte[] data)
  {
    String jsonStr = null;

    try
    {
      jsonStr = new String(data, "UTF-8");
    } catch (UnsupportedEncodingException var5)
    {
      var5.printStackTrace();
    }

    try
    {
      JSONObject jsonObj = new JSONObject(jsonStr);
      if (jsonObj.has("message"))
      {
        this.setMessage(jsonObj.optString("message"));
      }

      if (jsonObj.has("extra"))
      {
        this.setExtra(jsonObj.optString("extra"));
      }

      if (jsonObj.has("user"))
      {
        this.setUserInfo(this.parseJsonToUserInfo(jsonObj.getJSONObject("user")));
      }
    } catch (JSONException var4)
    {
      RLog.e("MyInformationNotificationMessage", "JSONException " + var4.getMessage());
    }

  }

  public int describeContents()
  {
    return 0;
  }

  public void writeToParcel(Parcel dest, int flags)
  {
    ParcelUtils.writeToParcel(dest, this.getMessage());
    ParcelUtils.writeToParcel(dest, this.getExtra());
    ParcelUtils.writeToParcel(dest, this.getUserInfo());
  }

  public MyInformationNotificationMessage(Parcel in)
  {
    this.setMessage(ParcelUtils.readFromParcel(in));
    this.setExtra(ParcelUtils.readFromParcel(in));
    this.setUserInfo((UserInfo) ParcelUtils.readFromParcel(in, UserInfo.class));
  }

  public MyInformationNotificationMessage(String message)
  {
    this.setMessage(message);
  }

  public String getMessage()
  {
    return this.message;
  }

  public void setMessage(String message)
  {
    this.message = message;
  }

  public String getExtra()
  {
    return this.extra;
  }

  public void setExtra(String extra)
  {
    this.extra = extra;
  }
}
